﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Data;
using TWS.Cart.Business;

public partial class Application_Controls_Detail : System.Web.UI.UserControl
{
	string permanentPath;

	protected int ProductId
	{
		get
		{
			if (!String.IsNullOrEmpty(Request.QueryString["prodId"]))
				return Convert.ToInt32(Request.QueryString["prodId"]);
			else
				return -1;
		}
	}


	protected void Page_Load(object sender, EventArgs e)
	{
		permanentPath = ConfigurationManager.AppSettings["FilePath"].ToString();

		CartCatProduct ssProduct = CartCatProduct.FetchByID(ProductId);

		if (ssProduct != null)
		{
			LiteralForProductName.Text = ssProduct.Name;
			LiteralForProductShortDescription.Text = ssProduct.DescriptionShort;
		}

		DrawImages();
		LiteralForProductBasicDetails.Text = DrawProductDetails(ssProduct);
    }

	protected string DrawProductDetails(CartCatProduct ssProd)
	{
		string res = "<table>";

		if (ssProd.Name != null)
			res += "<tr><td>" + Resources.Resource.Product + ":</td><td>" + ssProd.Name + "</td></tr>";
		if (ssProd.Sku != null)
			res += "<tr><td>" + Resources.Resource.Model + ":</td><td>" + ssProd.Sku + "</td></tr>";
		if (ssProd.Price != null)
			res += "<tr><td>" + Resources.Resource.Price + ":</td><td>" + string.Format("{0:C}",ssProd.Price) + "</td></tr>";

		if (ssProd.SizeWidth != null)
		{
			res += "<tr><td>" + Resources.Resource.Size + ":</td>";
			res +=      "<td><table>";
			res +=			"<tr><td>"+Resources.Resource.Width+":</td><td>" + string.Format("{0:##.##}",ssProd.SizeWidth) + "</td></tr>";
			res +=		    "<tr><td>"+Resources.Resource.Height+":</td><td>" + string.Format("{0:##.##}",ssProd.SizeHeight) + "</td></tr>";
			res += "</table></td>";
			res +=	"</tr>";
		}

		res += "</table>";

		return res;
	}
	

	protected void DrawImages()
	{
		CartTranProductsImageCollection images = new CartTranProductsImageCollection()
				.Where(CartTranProductsImage.Columns.ProductsId, ProductId)
				.Load();

		var mainImage = images.SingleOrDefault(x => x.IsDefault == true);

		if (mainImage != null)
		{
			ImageForMainPicture.ImageUrl = permanentPath + mainImage.FileName;
			ImageForMainPicture.ToolTip = mainImage.Title;
		}
	}
}
